package com.woniuxy.java102vuecli.consumer;

import cn.hutool.extra.mail.MailUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author author
 * @Create 2023-09-28 11:39
 */
@Component
public class MyConsumer {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @RabbitListener(queues="queueemail")
    public void receiveQueueEmail(String email, Message message, Channel channel) throws IOException {
        //生成随机验证码
        String code = UUID.randomUUID().toString().substring(0, 4);
        //存入redis,后续验证比较
        redisTemplate.opsForValue().set(email, code, 30, TimeUnit.MINUTES);
        //写入邮件内容
        MailUtil.send("chinasofti_lbs@163.com", "102测试", "重置密码验证码："+code, false);
        //手动确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
